<?php

/**
*	发帖后台
*
*/

//包含公共文件
include '../../common/common.php';

//接收post的传值
// var_dump($_POST);
$title      = trim($_POST['title']);
$content = trim($_POST['post_content']);
$rate      = trim($_POST['price']);
$pYzm    = trim($_POST['Pverify']);
//接收cid
$cid = $_POST['postCid'];
$uid = $_COOKIE['uid'];

//打印
// var_dump($title);
// var_dump($content);
// var_dump($rate);
// var_dump($pYzm);
// var_dump($uid);
// var_dump($cid);

//判断是否登录
if (!$_COOKIE['afterlogin']) {

	exit('亲 , 登录后才能发帖哟<br /> <a href="'  . $_SERVER['HTTP_REFERER'] .'">返回上一页</a>');
}

//判断帖子标题是否为空
if (!$title) {

	exit('帖子标题不得为空<br /> <a href="'  . $_SERVER['HTTP_REFERER'] .'">返回上一页</a>');
}

//判断标题长度
if (strlen($title) > 60) {

	exit('帖子标题不得超过60个字符,(或不超过20个汉字)<br /> <a href="'  . $_SERVER['HTTP_REFERER'] .'">返回上一页</a>');
}

//判断帖子内容是否为空
if (!$content) {

	exit('帖子内容不得为空<br /> <a href="'  . $_SERVER['HTTP_REFERER'] .'">返回上一页</a>');
}

//给空的价格赋值 0 
if (empty($rate)) {

	$rate = 0;
}

//判断帖子价格
if (!is_numeric($rate)) {

	exit('金钱必须是数字<br /> <a href="'  . $_SERVER['HTTP_REFERER'] .'">返回上一页</a>');
} 

if ($rate > 30 ||  $rate < 0) {

	exit('帖子价格应在0 ~ 30之间(不填则默认为 0 )<br /> <a href="'  . $_SERVER['HTTP_REFERER'] .'">返回上一页</a>');
}


//比对验证码
$verify = $_SESSION['verify'];
if ($pYzm !== $verify) {

	exit('验证码输入有误<br /> <a href="'  . $_SERVER['HTTP_REFERER'] .'">返回上一页</a>');
}


//获取 IP
$ip1 = $_SERVER['REMOTE_ADDR'];
if ($ip1 == '::1') {

	$ip1 = '127.0.0.1';
}
$ip = ip2long($ip1);

// var_dump($ip1);
// var_dump($ip);

//插入数据库
$data = [
	'first' 	    => 1,
	'title'	    => "$title",
	'content'   => "$content",
	'rate'	    => $rate,
	'addtime'  => time(),
	'authorid'  => $uid,
	'addip'      => $ip,
	'classid'    => $cid
];
//var_dump($data);
$resPost = myInsert($link , DB_TABLE_DETAIL , $data);
// var_dump($resPost);

//修改当前指定帖 (获取当前时间戳最大的数据, 并修改其lastpost=1,其余lastpost=0)
$resCurr = mySelect($link , '*' , DB_TABLE_DETAIL , "where  classid=$cid  and   first = 1 and isdel=0 order by addtime desc  limit  0 , 1 ");
$lastId = $resCurr[0]['id'];
$resLast = myUpdate($link , DB_TABLE_DETAIL , ['lastpost' => 1] , "id = $lastId");
$resOther = myUpdate($link , DB_TABLE_DETAIL , ['lastpost' => 0] , "classid=$cid  and  first=1  and id != $lastId");
//var_dump($resCurr);
// var_dump($resLast);
// var_dump($resOther);

//修改当前版块的帖子总条数 (直接从帖子详情页中计算总数)
$motifCount  = myCount($link , 'id', DB_TABLE_DETAIL , "where classid = $cid and first = 1");
$resAdd = myUpdate($link , DB_TABLE_CG , ['motifcount' => $motifCount] , "cid = $cid");
// var_dump($motifCount);
// var_dump($resAdd);

//查询用户当前积分和等级
$selGrade = mySelect($link , '*' ,DB_TABLE_USER , "where uid = $uid");
$grade = $selGrade[0]['grade'];
//加积分,并获取响应的等级数
$grade2 = $grade + 15; 
$grade3 = myLevel($grade2); 
$levName = $grade3[1];
$border    = $grade3[2];
// var_dump($grade3);
// var_dump($levName);
// var_dump($border);

//修改当前用户积分,等级
$dataLev  =  ['grade' => $grade2 , 'levname' => "$levName"];
$upGrade  = myUpdate($link , DB_TABLE_USER , $dataLev , "uid = $uid");
// var_dump($upGrade);

//判断是否刚升级
if ($grade < $border && $grade2 >= $border) {

	echo '恭喜您升级到<span style="color:red; font-weight:bold;"> ' . $levName . ' </span>';
}


//跳转页面
echo '<br />发表成功, 积分 <span style="color:red;"> +15</span>';
header('refresh:1;url=' . $webSite . 'public.php?cid=' . $cid);